
#include "add_dev.h"
#include "first_title.h"
static lv_timer_t * timer  = NULL;

static void net_state_timer(lv_timer_t * timer)
{
    // WiFi 联网状态
    // 0x00：空闲态
    // 0x01：待配网
    // 0x02：连接中
    // 0x03：已连接
    // 其他值：保留
    // if(2 == get_net_stage() )
    if(1)
    {
        //os_printf("page 1 get net stage----\n");
        page_install("cfg_net",lv_page_config_network_create(),NULL);
        page_push_anim("cfg_net",LOAD_ANIM_MOVE_LEFT);
    }
    // else if(3 == get_net_stage())
    // {
    //     os_printf("page 1 enter home----\n");
    //     com_title_create();
    //     root_page_enter_home(LOAD_ANIM_NONE);
    // }
}


static void ui_dev_switch_next_callback(lv_event_t* event)
{
    switch(event->code){
    // case LV_EVENT_CLICKED:
    // {
    //     debug_d("clicked\n");

    //     // page_install("cfg_net",lv_page_config_network_create(),NULL);
    //     // page_push_anim("cfg_net",LOAD_ANIM_MOVE_LEFT);

    //     break;
    // }
    case LV_EVENT_GESTURE:
    {
        lv_dir_t dir = LV_DIR_NONE;
        dir = lv_indev_get_gesture_dir(lv_indev_get_act());
        switch(dir){
        case LV_DIR_LEFT:{
            break;
        }
        case LV_DIR_RIGHT:{
            page_pop();
            first_title_set(FIRST_PAGE_DOWNLOAD,0,1);
            break;
        }
        default:break;
        }
    }
    default:break;
    }

    return ;
}

void ui_init_add_dev_page_src(lv_obj_t *superview)
{
    //******************************************************************
    lv_coord_t pw = 0;
    lv_coord_t ph = 0;

    first_title_set( FIRST_PAGE_ADD_DEV, 0, 2);
    //******************************************************************

    pw = lv_disp_get_hor_res(NULL);
    ph = lv_disp_get_ver_res(NULL);
    //******************************************************************
    lv_obj_t *bg  = lv_obj_create(superview);

    lv_obj_set_style_radius(bg, 0, LV_PART_MAIN);
	lv_obj_set_style_border_width(bg, 0, LV_PART_MAIN);
    lv_obj_set_style_border_opa(bg, LV_OPA_0, LV_PART_MAIN);
	lv_obj_set_style_border_color(bg, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
    lv_obj_set_style_pad_all(bg,0,0);
    lv_obj_set_scrollbar_mode(bg, LV_SCROLLBAR_MODE_OFF);

    #if 0
	lv_obj_set_style_bg_opa(bg, LV_OPA_0, LV_PART_MAIN);
    #else
	lv_obj_set_style_bg_opa(bg, LV_OPA_100, LV_PART_MAIN);
	lv_obj_set_style_bg_color(bg, lv_color_make(0,0,0), LV_PART_MAIN);
    #endif

	lv_obj_set_pos(bg,0,0);
	midea_ui_safe_set_size(bg,pw,ph);

    lv_obj_clear_flag(bg,LV_OBJ_FLAG_GESTURE_BUBBLE);
    lv_obj_add_event_cb(bg,ui_dev_switch_next_callback,LV_EVENT_ALL,NULL);
    //**************************************************** */
    lv_obj_t * img = lv_img_create(superview);
    midea_ui_set_img_src( img, pt_fs_get("AddDevice.png"));
    midea_ui_safe_obj_align( img,LV_ALIGN_TOP_LEFT,39,55);

    lv_obj_t * label = lv_label_create(superview);
    lv_obj_set_style_text_font( label, lv_font_get(20), LV_STATE_DEFAULT );
    lv_obj_set_style_text_color(label,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN);
    lv_obj_set_style_text_opa(label,LV_OPA_80,0);
    midea_ui_safe_set_text( label, "扫打开美的美居App\n点击右上角\"+\"\n添加设备");
    midea_ui_safe_obj_align( label, LV_ALIGN_TOP_LEFT,246,108);
    //**************************************************** */

    timer = lv_timer_create(net_state_timer, 500,  NULL);

}

static void add_dev_page_on_custom_attr_config(page_base_t* self)
{
    return ;
}
static void add_dev_page_on_view_load(page_base_t* self)
{
    ui_init_add_dev_page_src(self->root);
    return ;
}
static void add_dev_page_on_view_did_load(page_base_t* self)
{
    return ;
}
static void add_dev_page_on_view_will_appear(page_base_t* self)
{
    return ;
}
static void add_dev_page_on_view_did_appear(page_base_t* self)
{
    return ;
}
static void add_dev_page_on_view_will_disappear(page_base_t* self)
{
    return ;
}
static void add_dev_page_on_view_did_disappear(page_base_t* self)
{
    return ;
}
static void add_dev_page_on_view_did_unload(page_base_t* self)
{
    if(timer){
        midea_ui_safe_del_timer(timer);
        timer = NULL;
    }
    return ;
}

static page_vtable_t page_param = {
    .on_custom_attr_config  = add_dev_page_on_custom_attr_config,
    .on_view_load           = add_dev_page_on_view_load,
    .on_view_did_load       = add_dev_page_on_view_did_load,
    .on_view_will_appear    = add_dev_page_on_view_will_appear,
    .on_view_did_appear     = add_dev_page_on_view_did_appear,
    .on_view_will_disappear = add_dev_page_on_view_will_disappear,
    .on_view_did_disappear  = add_dev_page_on_view_did_disappear,
    .on_view_did_unload     = add_dev_page_on_view_did_unload,
};


page_vtable_t* lv_page_add_dev_create(void)
{
    return &page_param;
}
